home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: presence.py 671 2007-05-09 12:05:52Z jajcus $'
- __docformat__ = 'restructuredtext en'
- import libxml2
- from pyxmpp.utils import to_utf8, from_utf8
- from pyxmpp.stanza import Stanza
- from pyxmpp.xmlextra import common_ns
- presence_types = ('available', 'unavailable', 'probe', 'subscribe', 'unsubscribe', 'subscribed', 'unsubscribed', 'invisible', 'error')
- accept_responses = {
- 'subscribe': 'subscribed',
- 'subscribed': 'subscribe',
- 'unsubscribe': 'unsubscribed',
- 'unsubscribed': 'unsubscribe' }
- deny_responses = {
- 'subscribe': 'unsubscribed',
- 'subscribed': 'unsubscribe',
- 'unsubscribe': 'subscribed',
- 'unsubscribed': 'subscribe' }
-
- class Presence(Stanza):
- stanza_type = 'presence'
-
- def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None, stanza_id = None, show = None, status = None, priority = 0, error = None, error_cond = None, stream = None):
- self.xmlnode = None
- if isinstance(xmlnode, Presence):
- pass
- elif isinstance(xmlnode, Stanza):
- raise TypeError, "Couldn't make Presence from other Stanza"
- elif isinstance(xmlnode, libxml2.xmlNode):
- pass
- elif xmlnode is not None:
- raise TypeError, "Couldn't make Presence from %r" % (type(xmlnode),)
-
- if stanza_type and stanza_type not in presence_types:
- raise ValueError, 'Invalid presence type: %r' % (type,)
-
- if stanza_type == 'available':
- stanza_type = None
-
- if xmlnode is None:
- xmlnode = 'presence'
-
- Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type, stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)
- if show:
- self.xmlnode.newTextChild(common_ns, 'show', to_utf8(show))
-
- if status:
- self.xmlnode.newTextChild(common_ns, 'status', to_utf8(status))
-
- if priority and priority != 0:
- self.xmlnode.newTextChild(common_ns, 'priority', to_utf8(unicode(priority)))
-
-
-
- def copy(self):
- return Presence(self)
-
-
- def set_status(self, status):
- n = self.xpath_eval('ns:status')
- if not status:
- if n:
- n[0].unlinkNode()
- n[0].freeNode()
- else:
- return None
-
- if n:
- n[0].setContent(to_utf8(status))
- else:
- self.xmlnode.newTextChild(common_ns, 'status', to_utf8(status))
-
-
- def get_status(self):
- n = self.xpath_eval('ns:status')
- if n:
- return from_utf8(n[0].getContent())
- else:
- return None
-
-
- def get_show(self):
- n = self.xpath_eval('ns:show')
- if n:
- return from_utf8(n[0].getContent())
- else:
- return None
-
-
- def set_show(self, show):
- n = self.xpath_eval('ns:show')
- if not show:
- if n:
- n[0].unlinkNode()
- n[0].freeNode()
- else:
- return None
-
- if n:
- n[0].setContent(to_utf8(show))
- else:
- self.xmlnode.newTextChild(common_ns, 'show', to_utf8(show))
-
-
- def get_priority(self):
- n = self.xpath_eval('ns:priority')
- if not n:
- return 0
-
-
- try:
- prio = int(n[0].getContent())
- except ValueError:
- return 0
-
- return prio
-
-
- def set_priority(self, priority):
- n = self.xpath_eval('ns:priority')
- if not priority:
- if n:
- n[0].unlinkNode()
- n[0].freeNode()
- else:
- return None
-
- priority = int(priority)
- if priority < -128 or priority > 127:
- raise ValueError, 'Bad priority value'
-
- priority = str(priority)
- if n:
- n[0].setContent(priority)
- else:
- self.xmlnode.newTextChild(common_ns, 'priority', priority)
-
-
- def make_accept_response(self):
- if self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'):
- raise ValueError, "Results may only be generated for 'subscribe','subscribed','unsubscribe' or 'unsubscribed' presence"
-
- pr = Presence(stanza_type = accept_responses[self.get_type()], from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id())
- return pr
-
-
- def make_deny_response(self):
- if self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'):
- raise ValueError, "Results may only be generated for 'subscribe','subscribed','unsubscribe' or 'unsubscribed' presence"
-
- pr = Presence(stanza_type = deny_responses[self.get_type()], from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id())
- return pr
-
-
- def make_error_response(self, cond):
- if self.get_type() == 'error':
- raise ValueError, 'Errors may not be generated in response to errors'
-
- p = Presence(stanza_type = 'error', from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id(), error_cond = cond)
- if self.xmlnode.children:
- n = self.xmlnode.children
- while n:
- p.xmlnode.children.addPrevSibling(n.copyNode(1))
- n = n.next
-
- return p
-
-
-